#include "initdatabase.h"
#include "ui_initdatabase.h"

#include <QDir>
#include <QFileDialog>
#include <QProcess>
InitDataBase::InitDataBase(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::InitDataBase)
{
    ui->setupUi(this);
    initForm();
    
}

InitDataBase::~InitDataBase()
{
    delete ui;
}

void InitDataBase::initForm(){
    this->setWindowTitle("初始化数据库程序");
    //选择dminit路径
    connect(ui->btn_setDminitPath, &QPushButton::clicked, this, [=](){
        QString path = QFileDialog::getExistingDirectory(this, "选择dminit所在目录", QDir::homePath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
        if(!path.isEmpty()){
            ui->edit_dminitPath->setText(path);
        }
    });
    connect(ui->btn_setDataBasePath, &QPushButton::clicked, this, [=](){
        QString path = QFileDialog::getExistingDirectory(this, "选择数据库存放目录", QDir::homePath(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
        if(!path.isEmpty()){
            ui->edit_dataBasePath->setText(path);
        }
    });
    connect(ui->btn_startInitDataBase, &QPushButton::clicked, this, &InitDataBase::beginInit);
}

void InitDataBase::beginInit(){
    QProcess * process = new QProcess();
    QString shellFile = "./initDataBase.sh";
    QStringList args;
    args.append(ui->edit_dminitPath->text());
    args.append(ui->edit_dataBasePath->text());
    process->start(shellFile, args);
    process->close();
}
